-
Notifications
You must be signed in to change notification settings - Fork 745
/
BaseRemoteStorageProvider.cs
462 lines (364 loc) · 18.4 KB
/
BaseRemoteStorageProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information
namespace DotNetNuke.Providers.FolderProviders.Components
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Caching;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Host;
using DotNetNuke.Security;
using DotNetNuke.Services.FileSystem;
/// <inheritdoc />
public abstract class BaseRemoteStorageProvider : FolderProvider
{
private readonly string encryptionKey = Host.GUID;
private readonly PortalSecurity portalSecurity = PortalSecurity.Instance;
/// <inheritdoc/>
public override bool SupportsMappedPaths
{
get { return true; }
}
/// <inheritdoc/>
public override bool SupportsMoveFile
{
get { return false; }
}
/// <inheritdoc/>
public override bool SupportsMoveFolder
{
get { return true; }
}
protected virtual string FileNotFoundMessage
{
get { return string.Empty; }
}
protected virtual string ObjectCacheKey
{
get { return string.Empty; }
}
protected virtual int ObjectCacheTimeout
{
get { return 150; }
}
protected virtual string ListObjectsCacheKey
{
get { return string.Empty; }
}
protected virtual int ListObjectsCacheTimeout
{
get { return 300; }
}
/// <inheritdoc />
public override void AddFolder(string folderPath, FolderMappingInfo folderMapping)
{
this.AddFolder(folderPath, folderMapping, folderPath);
}
/// <inheritdoc />
public override void AddFile(IFolderInfo folder, string fileName, Stream content)
{
Requires.NotNull("folder", folder);
Requires.NotNullOrEmpty("fileName", fileName);
Requires.NotNull("content", content);
this.UpdateFile(folder, fileName, content);
}
public virtual void ClearCache(int folderMappingId)
{
var cacheKey = string.Format(this.ListObjectsCacheKey, folderMappingId);
DataCache.RemoveCache(cacheKey);
// Clear cached objects
DataCache.ClearCache(string.Format(this.ObjectCacheKey, folderMappingId, string.Empty));
}
/// <inheritdoc />
public override void CopyFile(string folderPath, string fileName, string newFolderPath, FolderMappingInfo folderMapping)
{
Requires.NotNull("folderPath", folderPath);
Requires.NotNullOrEmpty("fileName", fileName);
Requires.NotNull("newFolderPath", newFolderPath);
Requires.NotNull("folderMapping", folderMapping);
if (folderPath == newFolderPath)
{
return;
}
this.CopyFileInternal(folderMapping, folderPath + fileName, newFolderPath + fileName);
}
/// <inheritdoc />
public override void DeleteFile(IFileInfo file)
{
Requires.NotNull("file", file);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(file.PortalId, file.FolderMappingID);
var folder = FolderManager.Instance.GetFolder(file.FolderId);
this.DeleteFileInternal(folderMapping, folder.MappedPath + file.FileName);
}
/// <remarks>Azure Storage doesn't support folders, but we need to delete the file added while creating the folder.</remarks>
/// <inheritdoc />
public override void DeleteFolder(IFolderInfo folder)
{
Requires.NotNull("folder", folder);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(folder.PortalID, folder.FolderMappingID);
this.DeleteFolderInternal(folderMapping, folder);
}
/// <inheritdoc />
public override bool FileExists(IFolderInfo folder, string fileName)
{
Requires.NotNull("folder", folder);
Requires.NotNullOrEmpty("fileName", fileName);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(folder.PortalID, folder.FolderMappingID);
var item = this.GetStorageItem(folderMapping, folder.MappedPath + fileName);
return item != null;
}
/// <inheritdoc />
public override bool FolderExists(string folderPath, FolderMappingInfo folderMapping)
{
Requires.NotNull("folderPath", folderPath);
Requires.NotNull("folderMapping", folderMapping);
// the root folder should always exist.
if (folderPath == string.Empty)
{
return true;
}
var list = this.GetObjectList(folderMapping, folderPath);
return list.Any(o => o.Key.StartsWith(folderPath, StringComparison.InvariantCultureIgnoreCase));
}
/// <remarks>Amazon doesn't support file attributes.</remarks>
/// <inheritdoc />
public override FileAttributes? GetFileAttributes(IFileInfo file)
{
return null;
}
/// <inheritdoc />
public override string[] GetFiles(IFolderInfo folder)
{
Requires.NotNull("folder", folder);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(folder.PortalID, folder.FolderMappingID);
var list = this.GetObjectList(folderMapping, folder.MappedPath);
var mappedPath = folder.MappedPath;
// return (from i in list
// let f = i.Key
// let r = (!string.IsNullOrEmpty(mappedPath) ? f.Replace(mappedPath, "") : f)
// where f.StartsWith(mappedPath, true, CultureInfo.InvariantCulture) && f.Length > mappedPath.Length && r.IndexOf("/", StringComparison.Ordinal) == -1
// select Path.GetFileName(f)).ToArray();
var pattern = "^" + mappedPath;
return (from i in list
let f = i.Key
let r = !string.IsNullOrEmpty(mappedPath) ? Regex.Replace(f, pattern, string.Empty, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(2)) : f
where f.StartsWith(mappedPath, true, CultureInfo.InvariantCulture) && f.Length > mappedPath.Length && r.IndexOf("/", StringComparison.Ordinal) == -1
select Path.GetFileName(f)).ToArray();
}
/// <inheritdoc />
public override long GetFileSize(IFileInfo file)
{
Requires.NotNull("file", file);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(file.PortalId, file.FolderMappingID);
var folder = FolderManager.Instance.GetFolder(file.FolderId);
var item = this.GetStorageItem(folderMapping, folder.MappedPath + file.FileName);
if (item == null)
{
throw new FileNotFoundException(this.FileNotFoundMessage, file.RelativePath);
}
return item.Size;
}
/// <inheritdoc />
public override Stream GetFileStream(IFileInfo file)
{
Requires.NotNull("file", file);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(file.PortalId, file.FolderMappingID);
var folder = FolderManager.Instance.GetFolder(file.FolderId);
return this.GetFileStreamInternal(folderMapping, folder.MappedPath + file.FileName);
}
/// <inheritdoc />
public override Stream GetFileStream(IFolderInfo folder, string fileName)
{
Requires.NotNull("folder", folder);
Requires.NotNullOrEmpty("fileName", fileName);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(folder.PortalID, folder.FolderMappingID);
return this.GetFileStreamInternal(folderMapping, folder.MappedPath + fileName);
}
/// <inheritdoc />
public override DateTime GetLastModificationTime(IFileInfo file)
{
Requires.NotNull("file", file);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(file.PortalId, file.FolderMappingID);
var folder = FolderManager.Instance.GetFolder(file.FolderId);
var item = this.GetStorageItem(folderMapping, folder.MappedPath + file.FileName);
if (item == null)
{
throw new FileNotFoundException(this.FileNotFoundMessage, file.RelativePath);
}
return item.LastModified;
}
/// <inheritdoc />
public override IEnumerable<string> GetSubFolders(string folderPath, FolderMappingInfo folderMapping)
{
Requires.NotNull("folderPath", folderPath);
Requires.NotNull("folderMapping", folderMapping);
var list = this.GetObjectList(folderMapping, folderPath);
var pattern = "^" + Regex.Escape(folderPath);
return (from o in list
let f = o.Key
let r =
!string.IsNullOrEmpty(folderPath)
? Regex.Replace(f, pattern, string.Empty, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(2))
: f
where f.StartsWith(folderPath, StringComparison.InvariantCultureIgnoreCase)
&& f.Length > folderPath.Length
&& r.IndexOf("/", StringComparison.Ordinal) > -1
select folderPath + r.Substring(0, r.IndexOf("/", StringComparison.Ordinal)) + "/").Distinct().ToList();
// var mylist = (from o in list
// let f = o.Key
// let r = (!string.IsNullOrEmpty(folderPath) ? RegexUtils.GetCachedRegex(Regex.Escape(folderPath)).Replace(f, string.Empty, 1) : f)
// where f.StartsWith(folderPath)
// && f.Length > folderPath.Length
// && r.IndexOf("/", StringComparison.Ordinal) > -1
// select folderPath + r.Substring(0, r.IndexOf("/", StringComparison.Ordinal)) + "/").Distinct().ToList();
// return mylist;
}
/// <remarks>For now, it returns false always until we find a better way to check if the file is synchronized.</remarks>
/// <inheritdoc />
public override bool IsInSync(IFileInfo file)
{
return Convert.ToInt32((file.LastModificationTime - this.GetLastModificationTime(file)).TotalSeconds) == 0;
}
/// <inheritdoc />
public override void MoveFolder(string folderPath, string newFolderPath, FolderMappingInfo folderMapping)
{
Requires.NotNullOrEmpty("folderPath", folderPath);
Requires.NotNullOrEmpty("newFolderPath", newFolderPath);
Requires.NotNull("folderMapping", folderMapping);
this.MoveFolderInternal(folderMapping, folderPath, newFolderPath);
}
/// <inheritdoc />
public override void RenameFile(IFileInfo file, string newFileName)
{
Requires.NotNull("file", file);
Requires.NotNullOrEmpty("newFileName", newFileName);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(file.PortalId, file.FolderMappingID);
var folder = FolderManager.Instance.GetFolder(file.FolderId);
this.MoveFileInternal(folderMapping, folder.MappedPath + file.FileName, folder.MappedPath + newFileName);
}
/// <inheritdoc />
public override void RenameFolder(IFolderInfo folder, string newFolderName)
{
Requires.NotNull("folder", folder);
Requires.NotNullOrEmpty("newFolderName", newFolderName);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(folder.PortalID, folder.FolderMappingID);
var mappedPath = folder.MappedPath;
if (mappedPath == folder.FolderName + "/" || mappedPath.EndsWith("/" + folder.FolderName + "/", StringComparison.Ordinal))
{
var newMappedPath =
PathUtils.Instance.FormatFolderPath(
mappedPath.Substring(0, mappedPath.LastIndexOf(folder.FolderName, StringComparison.Ordinal)) +
newFolderName);
this.MoveFolderInternal(folderMapping, mappedPath, newMappedPath);
}
}
/// <remarks>No implementation needed because this provider doesn't support <see cref="FileAttributes"/>.</remarks>
/// <inheritdoc />
public override void SetFileAttributes(IFileInfo file, FileAttributes fileAttributes)
{
}
/// <remarks>Amazon doesn't support file attributes.</remarks>
/// <inheritdoc />
public override bool SupportsFileAttributes()
{
return false;
}
/// <inheritdoc />
public override void UpdateFile(IFileInfo file, Stream content)
{
Requires.NotNull("file", file);
Requires.NotNull("content", content);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(file.PortalId, file.FolderMappingID);
var folder = FolderManager.Instance.GetFolder(file.FolderId);
this.UpdateFileInternal(content, folderMapping, folder.MappedPath + file.FileName);
}
/// <inheritdoc />
public override void UpdateFile(IFolderInfo folder, string fileName, Stream content)
{
Requires.NotNull("folder", folder);
Requires.NotNullOrEmpty("fileName", fileName);
Requires.NotNull("content", content);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(folder.PortalID, folder.FolderMappingID);
this.UpdateFileInternal(content, folderMapping, folder.MappedPath + fileName);
}
/// <inheritdoc/>
public override string GetHashCode(IFileInfo file)
{
Requires.NotNull("file", file);
var folder = FolderManager.Instance.GetFolder(file.FolderId);
var folderMapping = FolderMappingController.Instance.GetFolderMapping(folder.PortalID, folder.FolderMappingID);
var item = this.GetStorageItem(folderMapping, folder.MappedPath + file.FileName);
if (item == null)
{
throw new FileNotFoundException(this.FileNotFoundMessage, file.RelativePath);
}
return (item.Size == file.Size) ? item.HashCode : string.Empty;
}
/// <inheritdoc/>
public override string GetHashCode(IFileInfo file, Stream fileContent)
{
if (this.FileExists(FolderManager.Instance.GetFolder(file.FolderId), file.FileName))
{
return this.GetHashCode(file);
}
return string.Empty;
}
protected static bool GetBooleanSetting(FolderMappingInfo folderMapping, string settingName)
{
return bool.Parse(folderMapping.FolderMappingSettings[settingName].ToString());
}
protected static int GetIntegerSetting(FolderMappingInfo folderMapping, string settingName, int defaultValue)
{
int value;
if (int.TryParse(GetSetting(folderMapping, settingName), out value))
{
return value;
}
return defaultValue;
}
protected static string GetSetting(FolderMappingInfo folderMapping, string settingName)
{
Requires.NotNull(nameof(folderMapping), folderMapping);
Requires.NotNullOrEmpty(nameof(settingName), settingName);
return folderMapping.FolderMappingSettings[settingName]?.ToString();
}
protected abstract void CopyFileInternal(FolderMappingInfo folderMapping, string sourceUri, string newUri);
protected abstract void DeleteFileInternal(FolderMappingInfo folderMapping, string uri);
protected abstract void DeleteFolderInternal(FolderMappingInfo folderMapping, IFolderInfo folder);
protected abstract Stream GetFileStreamInternal(FolderMappingInfo folderMapping, string uri);
protected abstract IList<IRemoteStorageItem> GetObjectList(FolderMappingInfo folderMapping);
protected virtual IList<IRemoteStorageItem> GetObjectList(FolderMappingInfo folderMapping, string path)
{
return this.GetObjectList(folderMapping);
}
protected abstract void MoveFileInternal(FolderMappingInfo folderMapping, string sourceUri, string newUri);
protected abstract void MoveFolderInternal(FolderMappingInfo folderMapping, string sourceUri, string newUri);
protected abstract void UpdateFileInternal(Stream stream, FolderMappingInfo folderMapping, string uri);
protected virtual IRemoteStorageItem GetStorageItem(FolderMappingInfo folderMapping, string key)
{
return this.GetStorageItemInternal(folderMapping, key);
}
private IRemoteStorageItem GetStorageItemInternal(FolderMappingInfo folderMapping, string key)
{
var cacheKey = string.Format(this.ObjectCacheKey, folderMapping.FolderMappingID, key);
return CBO.GetCachedObject<IRemoteStorageItem>(
new CacheItemArgs(
cacheKey,
this.ObjectCacheTimeout,
CacheItemPriority.Default,
folderMapping.FolderMappingID),
c =>
{
var list = this.GetObjectList(folderMapping, key);
// return list.FirstOrDefault(i => i.Key == key);
return list.FirstOrDefault(i => i.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase));
});
}
}
}